﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CocisDAL;
using CocisModel;
using CocisModel.Common;
using System.Data;
using CocisWeb.src;

namespace CocisWeb.ManageAsset
{
    public partial class AddServer : BasePage
    {
        #region Fields

        protected AssetDAL dal = new AssetDAL();
        protected LogsDAL logs = new LogsDAL();

        #endregion

        #region  Page Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindData();
            }
        }

        #endregion

        #region Bind Data

        protected void BindData()
        {
            BindFilters();
            BindDropDownList();
            //BindPhysicalMachineGridView();
        }

        protected void BindFilters()
        { 
            this.StatusDropDownList.DataTextField = "Status";
            this.StatusDropDownList.DataValueField = "StatusID";
            this.StatusDropDownList.DataSource = dal.GetStatus().Tables[0];
            this.StatusDropDownList.DataBind();

            this.ServerTypeDropDownList.DataValueField = "ServerTypeID";
            this.ServerTypeDropDownList.DataTextField = "ServerType";
            this.ServerTypeDropDownList.DataSource = dal.GetServerType().Tables[0];
            this.ServerTypeDropDownList.DataBind();
        }

        protected void BindDropDownList()
        {
            this.ddlDomain.DataSource = dal.GetDomain();
            this.ddlDomain.DataTextField = "Domain";
            this.ddlDomain.DataValueField = "DomainID";
            this.ddlDomain.DataBind();

            this.ddlOSName.DataSource = dal.GetOSName();
            this.ddlOSName.DataTextField = "OSName";
            this.ddlOSName.DataValueField = "OSNameID";
            this.ddlOSName.DataBind();

            this.ddlOSServicePack.DataSource = dal.GetOSServicePack();
            this.ddlOSServicePack.DataTextField = "OSServicePack";
            this.ddlOSServicePack.DataValueField = "OSServicePackID";
            this.ddlOSServicePack.DataBind();

            this.ddlEnvironment.DataSource = dal.GetEnvironment();
            this.ddlEnvironment.DataTextField = "Environment";
            this.ddlEnvironment.DataValueField = "EnvironmentID";
            this.ddlEnvironment.DataBind();

            this.ddlStatus.DataSource = dal.GetStatus();
            this.ddlStatus.DataTextField = "Status";
            this.ddlStatus.DataValueField = "StatusID";
            this.ddlStatus.DataBind();

            this.ddlScomStatus.DataSource = dal.GetScomStatus();
            this.ddlScomStatus.DataTextField = "ScomStatus";
            this.ddlScomStatus.DataValueField = "ScomStatusID";
            this.ddlScomStatus.DataBind();

            this.ddlPatchStatus.DataSource = dal.GetPatchStatus();
            this.ddlPatchStatus.DataTextField = "PatchStatus";
            this.ddlPatchStatus.DataValueField = "PatchStatusID";
            this.ddlPatchStatus.DataBind();

            this.ddlCVCoverage.DataSource = dal.GetCVCoverage();
            this.ddlCVCoverage.DataTextField = "CVCoverage";
            this.ddlCVCoverage.DataValueField = "CVCoverageID";
            this.ddlCVCoverage.DataBind();

            this.ddlHostingType.DataSource = dal.GetHostingType();
            this.ddlHostingType.DataTextField = "HostingType";
            this.ddlHostingType.DataValueField = "HostingTypeID";
            this.ddlHostingType.DataBind();

            this.ddlBU.DataSource = dal.GetBU();
            this.ddlBU.DataTextField = "BU";
            this.ddlBU.DataValueField = "BUID";
            this.ddlBU.DataBind();

            this.ddlServerType.DataSource = dal.GetServerType();
            this.ddlServerType.DataTextField = "ServerType";
            this.ddlServerType.DataValueField = "ServerTypeID";
            this.ddlServerType.DataBind();

            this.ddlClusterName.DataSource = dal.GetClusterServer();
            this.ddlClusterName.DataTextField = "ClusterID";
            this.ddlClusterName.DataValueField = "ClusterName";
            this.ddlClusterName.DataBind();
        }

        #endregion

        //点击新增按钮后的动作
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Asset.tbServerDataTable dt = new Asset.tbServerDataTable();
            Asset.tbServerRow rows = dt.NewtbServerRow();
            Guid gd = new Guid();
            rows.ServerID = gd;
            rows.PhysicalID = new Guid(this.txtPhysicalID.Text.ToString());
            rows.ServerName = this.txtServerName.Text;
            rows.Alias = this.txtAlias.Text;
            rows.ServerIP = this.txtServerIP.Text;
            rows.DomainID = int.Parse(this.ddlDomain.SelectedValue.ToString());
            rows.OSNameID = int.Parse(this.ddlOSName.SelectedValue.ToString());
            rows.OSVersion = this.txtOSVersion.Text;
            rows.OSServicePackID = int.Parse(this.ddlOSServicePack.SelectedValue.ToString());
            rows.EnvironmentID = int.Parse(this.ddlEnvironment.SelectedValue.ToString());
            rows.CPU = this.txtCPU.Text;
            rows.RAM = this.txtRAM.Text;
            rows.HardDisk = this.txtHardDisk.Text;
            rows.StatusID = int.Parse(this.ddlStatus.SelectedValue.ToString());
            rows.ScomStatusID = int.Parse(this.ddlScomStatus.SelectedValue.ToString());
            rows.PatchStatusID = int.Parse(this.ddlPatchStatus.SelectedValue.ToString());
            rows.CVCoverageID = int.Parse(this.ddlCVCoverage.SelectedValue);
            rows.AppCatalog = this.txtAppCatalog.Text;
            rows.HostingTypeID = int.Parse(this.ddlHostingType.SelectedValue.ToString());
            rows.AppSupportTeam = this.txtAppSupportTeam.Text;
            rows.BuID = int.Parse(this.ddlBU.SelectedValue.ToString());
            rows.BUdetails = this.txtBUdetails.Text;
            rows.IfCluster = this.txtIfCluster.Text;
            rows.ServerTypeID = int.Parse(this.ddlServerType.SelectedValue.ToString());
            rows.ClusterID = int.Parse( this.ddlClusterName.SelectedValue);  

            dt.Rows.Add(rows);
            dt.AcceptChanges();

            int temp = dal.AddServer(dt);
            if (temp > 0)
            {
                string ip = Request.UserHostAddress.ToString();
                int t = logs.AddNewLogsInfo("insert a new Server named " + rows.ServerName, LoginUser.UserName, ip);

                ClientScript.RegisterStartupScript(this.GetType(), "a", "<script language=javascript>alert('添加成功！')</script>");
                Response.Redirect("AddServer.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "b", "<script language=javascript>alert('添加失败！')</script>");
            }

        }

        #region  Bind Data

        protected void BindPhysicalMachineGridView()
        {
            DateTime warrantyStartDate = DateTime.MinValue;
            DateTime warrantyEndData = DateTime.MaxValue;
            if (!String.IsNullOrEmpty(txtWarrantyStartDate.Text))
            {
                DateTime.TryParse(txtWarrantyStartDate.Text, out warrantyStartDate);
            }
            if (!String.IsNullOrEmpty(txtWarrantyEndDate.Text))
            {               
                DateTime.TryParse(txtWarrantyEndDate.Text, out warrantyEndData);
            } 
            int serverTypeID = 0;
            Int32.TryParse(ServerTypeDropDownList.SelectedValue, out serverTypeID);
            int statusID =0;
            Int32.TryParse(StatusDropDownList.SelectedValue,out statusID);
            string iloip = iLOIPTextBox.Text.Trim();
            string brand = BrandTextBox.Text.Trim();
            DataSet dset = dal.GetPhysicalServers(serverTypeID, statusID, iloip, brand, warrantyStartDate, warrantyEndData);
            selectPhysicalMachineGridView.DataSource = dset.Tables[0];
            selectPhysicalMachineGridView.DataBind();
        }

        #endregion

        #region Search
        protected void SearchButton_Click(object sender, EventArgs e)
        {
            BindPhysicalMachineGridView();
        }
        #endregion

        #region Events

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            selectPhysicalMachineGridView.RowDataBound += new GridViewRowEventHandler(selectPhysicalMachine_RowDataBound);
            selectPhysicalMachineGridView.RowCommand += new GridViewCommandEventHandler(selectPhysicalMachineGridView_RowCommand);
        }

        void selectPhysicalMachine_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int totalColumnCount = e.Row.Cells.Count;
                Button selectButton = e.Row.Cells[totalColumnCount - 1].FindControl("SelectButton") as Button;
                selectButton.CommandArgument = DataBinder.Eval(e.Row.DataItem, "PhysicalID").ToString();
            }
        }

        void selectPhysicalMachineGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                this.selectPhysicalMachinePanel.Visible = false;
                txtPhysicalID.Text = e.CommandArgument.ToString();
                this.editServerPanel.Visible = true;
            }
        }

        #endregion

    }
}
